package fhp.hlhj.giantfold.activity.annualized
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.widget.RelativeLayout
import cn.sharesdk.framework.Platform
import cn.sharesdk.framework.PlatformActionListener
import cn.sharesdk.onekeyshare.OnekeyShare
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.ShowAnnualViewPagerAdp
import fhp.hlhj.giantfold.utils.LogUtil
import kotlinx.android.synthetic.main.activity_show_annual_aty.*
import java.util.HashMap
import android.os.Environment.getExternalStorageDirectory
import java.io.File
import com.czt.mp3recorder.util.LameUtil.flush
import android.R.attr.bitmap
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.makeramen.roundedimageview.RoundedImageView
import fhp.hlhj.giantfold.utils.MyUtils
import java.io.FileOutputStream
import java.io.IOException
import java.util.ArrayList

class ShowAnnualAty : BaseActivity() {
    private lateinit var pagerAdapter:ShowAnnualViewPagerAdp
    private val map=HashMap<Int,Boolean>()
    private val rDatas=ArrayList<String>()
    override fun getContentId(): Int = R.layout.activity_show_annual_aty
    var PHOTO_DIR = File(getExternalStorageDirectory().toString() + "/juzheImg")//设置保存路径
    var avaterFile = File(PHOTO_DIR, "avater.jpg")//设置文件名称
    @RequiresApi(Build.VERSION_CODES.N)
    override fun initView() {
        viewPager.pageMargin = resources.getDimensionPixelSize(R.dimen.PagerMargin)
        viewPager.offscreenPageLimit = 3
        pagerAdapter=ShowAnnualViewPagerAdp(this)
        viewPager.adapter=pagerAdapter
        rDatas.add("1")
        rDatas.add("1")
        rDatas.add("1")
        rDatas.add("1")
        rDatas.add("1")
        rDatas.add("1")
        map.put(0,true)
        map.put(1,false)
        map.put(2,false)
        map.put(3,false)
        map.put(4,false)
        map.put(5,false)
        btCommit.setOnClickListener {

            val view = viewPager.getChildAt(viewPager.currentItem) as RelativeLayout
            val view1 = view.findViewById<RelativeLayout>(R.id.lo)
            val view2 = view1.findViewById<RelativeLayout>(R.id.lo1)
//            view2.isDrawingCacheEnabled = true
//            view2.buildDrawingCache()
//            var bitmap = view2.drawingCache
            val bitmap1 = MyUtils.getViewBp(view2)
            saveBitmap(bitmap1)
        }

        val recyclerAdp=object :BaseQuickAdapter<String,BaseViewHolder>(R.layout.anu_item,rDatas){
            override fun convert(helper: BaseViewHolder?, item: String?) {
                val imageView = helper?.getView<ImageView>(R.id.img)
                val bg = helper?.getView<TextView>(R.id.bg)
                if (map[helper?.adapterPosition]!!){
                    bg!!.visibility= View.VISIBLE
                }else{
                    bg!!.visibility= View.GONE
                }
                when(helper?.adapterPosition){
                    0->{
                            imageView?.setImageResource(R.mipmap.zt_imgs)
                    }
                    1->{
                        imageView?.setImageResource(R.mipmap.zt_imgf)
                    }
                    2->{
                        imageView?.setImageResource(R.mipmap.zt_imgo)
                    }
                    3->{
                        imageView?.setImageResource(R.mipmap.zt_imgfi)
                    }
                    4->{
                        imageView?.setImageResource(R.mipmap.zt_imgsx)
                    }
                    5->{
                        imageView?.setImageResource(R.mipmap.zt_imgt)
                    }
                }
            }
        }
        recyclerAdp.setOnItemClickListener { adapter, view, position ->
            map.forEach { t, u ->
                if (t==position){
                    map.put(t,true)
                }else{
                    map.put(t,false)
                }
            }
            viewPager.setCurrentItem(position)
            recyclerAdp.notifyDataSetChanged()
        }
        recyclerview.adapter=recyclerAdp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayout.HORIZONTAL,false)

    }
    private fun saveBitmap(bitmap: Bitmap){
        if (!PHOTO_DIR.exists()){
            PHOTO_DIR.mkdir()
        }
        if (avaterFile.exists()) {
            avaterFile.delete()
        }
        try {
            avaterFile.createNewFile()
            val fos = FileOutputStream(avaterFile)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
            fos.flush()
            fos.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
        showShare()

    }

    private fun showShare() {
        val oks = OnekeyShare()

        oks.callback=object : PlatformActionListener {
            override fun onComplete(p0: Platform?, p1: Int, p2: HashMap<String, Any>?) {
            }

            override fun onCancel(p0: Platform?, p1: Int) {
            }

            override fun onError(p0: Platform?, p1: Int, p2: Throwable?) {
                LogUtil.log("错误$p2")
            }
        }
        LogUtil.log(avaterFile.path)
        oks.setImagePath(avaterFile.path)
//        oks.setImagePath(avaterFile.path)
        //关闭sso授权
        oks.disableSSOWhenAuthorize()
        // title标题，印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
//        oks.setTitle("邀请您参加巨折")
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
//        oks.setTitleUrl("http://pintu.hlhjapp.com/index.php?m=home&c=user&a=inviteCode&username=" )
        // text是分享文本，所有平台都需要这个字段
//        oks.text = "扫描获得邀请码，注册成功后，邀请你的用户将获得红包奖励！"
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg")
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
//        oks.setUrl("http://pintu.hlhjapp.com/index.php?m=home&c=user&a=inviteCode&username=" )
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本")
        // site是分享此内容的网站名称，仅在QQ空间使用
//        oks.setSite("ShareSDK")
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
//        oks.setSiteUrl("http://pintu.hlhjapp.com/index.php?m=home&c=user&a=inviteCode&username=")
        // 启动分享GUI
        oks.show(this)

    }

    override fun initListener() {
        btExit.setOnClickListener {
            finish()
        }
    }

}
